const path = require("path");
const { whenProd, getPlugin, pluginByName } = require("@craco/craco");
module.exports = {
  webpack: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
    //配置webpack
    //配置CDN
    configure: (webpackConfig) => {
      let cdn;
      whenProd(() => {
        // 设置 externals，告诉 webpack 不要打包这些库
        webpackConfig.externals = {
          react: "React",
          "react-dom": "ReactDOM",
        };

        // 定义 CDN 链接，需购买CDN-迪科网络不行，手机开热点
        cdn = {
          js: [
            "https://cdn.jsdelivr.net/npm/react@18.2.0/umd/react.production.min.js",
            "https://cdn.jsdelivr.net/npm/react-dom@18.2.0/umd/react-dom.production.min.js",
          ],
        };
      });

      // 将 CDN 配置注入到 HtmlWebpackPlugin
      const { isFound, match } = getPlugin(
        webpackConfig,
        pluginByName("HtmlWebpackPlugin")
      );
      if (isFound) {
        match.options.cdn = cdn;
      }

      return webpackConfig;
    },
  },
};
